package net.huashitong.index.ctrl;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.security.SysPrincipal;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.index.dataModel.IndexTargetDataModel;
import net.huashitong.index.entity.DepartmentEntity;
import net.huashitong.index.entity.EnterpriseEntity;
import net.huashitong.index.entity.IndexInfoEntity;
import net.huashitong.index.entity.IndexTargetEntity;
import net.huashitong.index.filter.DepartmentFilter;
import net.huashitong.index.filter.EnterpriseFilter;
import net.huashitong.index.filter.IndexTargetFilter;
import net.huashitong.index.service.DepartmentService;
import net.huashitong.index.service.EnterpriseService;
import net.huashitong.index.service.IndexInfoService;
import net.huashitong.index.service.IndexTargetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/indexTarget")
public class IndexTargetCtrl extends JsonBaseController {

    @Autowired
    private IndexTargetService indexTargetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private IndexInfoService indexInfoService;

    @Override
    protected String getView(String s) {
        return "/modules/sys/eco/indexTarget/" + s;
    }

    //载入初始数据
    @RequestMapping(method = RequestMethod.GET)
    public String initPage(HttpServletRequest request, Model model) {
        return getView("query");
    }

    @ResponseBody
    @RequiresPermissions("web:indexTarget:query")
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute IndexTargetDataModel filter) {
        List<IndexTargetDataModel> query = indexTargetService.queryByFilter(filter);
        return coverJqGrid(filter, query);
    }

    /**
     * 添加指标目标页面
     * @param filter
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/addTarget", method = RequestMethod.GET)
    public String addTarget(@ModelAttribute IndexTargetFilter filter, Model model, HttpServletRequest request) {
        //指标
        List<IndexInfoEntity> indexInfoEntityList = indexInfoService.query();

        model.addAttribute("indexInfoEntityList", indexInfoEntityList);
        model.addAttribute("target", filter);
        return getView("addTarget");
    }

    /**
     * 添加指标目标
     * @param filter
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addTarget", method = RequestMethod.POST)
    public Message addTarget(@ModelAttribute IndexTargetDataModel filter, HttpServletRequest request) {
        //查询指标
        IndexInfoEntity indexInfo = indexInfoService.getById(filter.getIndexId());
        IndexTargetEntity target = new IndexTargetEntity();
        target.setIndexId(filter.getIndexId());
        //默认设置目标值指标时间类型为1月度 2季度 3年度
        target.setDateUnit(indexInfo.getDateUnit());
        //默认指标状态1
        target.setIndexState("1");
        //默认目标值类型1（完成值）
        target.setValueType(1);
        //目标值
        target.setTargetValue(filter.getTargetValue() + "万元");
        //目标时间
        target.setTargetTime(filter.getTargetTime());

        SysPrincipal principal = SysUserUtils.getSysPrincipal();
        String name = principal.getName();
        //目标来源
        target.setSource(name);
        indexTargetService.save(target);
        return coverMessage("200");
    }

    /**
     * 删除指标目标
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delTarget/{id}", method = RequestMethod.POST)
    public Message delTarget(@PathVariable List<String> id) {
        indexTargetService.batchDelete(id);
        return coverMessage("200");
    }

    /**
     * 修改目标指标
     * @param model
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "/editTarget/{id}", method = RequestMethod.GET)
    public String showEdit(Model model, HttpServletRequest request, @PathVariable String id) {
        IndexTargetEntity target = indexTargetService.getById(id);
        model.addAttribute("target", target);
        return getView("form");
    }

    /**
     * 修改目标指标
     * @param filter
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editTarget", method = RequestMethod.POST)
    public Message editTarget(@ModelAttribute IndexTargetFilter filter, HttpServletRequest request) {
        IndexTargetEntity target = indexTargetService.getById(filter.getSysId());
        target.setTargetValue(filter.getTargetValue());
        target.setTargetTime(filter.getTargetTime());
        indexTargetService.update(target);
        return coverMessage("200");
    }

    /**
     * 根据指标id查询指标实体
     * @param filter
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryBeanByIndexId", method = RequestMethod.POST)
    public Message queryBeanByIndexId(@RequestBody IndexTargetDataModel filter, HttpServletRequest request) {
        if (StringUtils.isNotBlank(filter.getIndexId())) {
            IndexInfoEntity indexInfo = indexInfoService.getById(filter.getIndexId());
            //封装部门map
            List<DepartmentEntity> deptList = departmentService.query();
            Map<String, DepartmentEntity> deptMap = new HashMap<>();
            for (DepartmentEntity dept : deptList) {
                if (!deptMap.containsKey(dept.getDeptCode())) {
                    deptMap.put(dept.getDeptCode(), dept);
                }
            }
            //封装企业map
            List<EnterpriseEntity> entities = enterpriseService.query();
            Map<String, EnterpriseEntity> enterpriseMap = new HashMap<>();
            for (EnterpriseEntity enterprise : entities) {
                if (!enterpriseMap.containsKey(enterprise.getSysId())) {
                    enterpriseMap.put(enterprise.getSysId(), enterprise);
                }
            }
            //根据指标id查询指标实体
            Map<String, Object> map = indexInfoService.queryBeanById(indexInfo, deptMap, enterpriseMap);
            if (ObjectUtils.isNotBlank(map)) {
                return coverMessage("200", "请求成功", map);
            } else {
                return coverMessage("201");
            }
        } else {
            return coverMessage("201");
        }
    }

}
